package com.zzh.takeoutapp.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.Toast;

import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXImageObject;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXTextObject;
import com.tencent.mm.opensdk.modelmsg.WXVideoObject;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.zzh.takeoutapp.R;
import com.zzh.takeoutapp.app.MainApp;
import com.zzh.takeoutapp.config.Constants;

/**
 * Created by xmg on 2016/11/29.
 */

public class WxShareAndLoginUtils {

    public static int WECHAT_FRIEND = 0;  //分享好友
    public static int WECHAT_MOMENT = 1;  //分享朋友圈
    private static IWXAPI iwxapi;

    public static IWXAPI getWXAPI() {
        if (iwxapi == null) {
            //通过WXAPIFactory创建IWAPI实例
            iwxapi = WXAPIFactory.createWXAPI(MainApp.getContext(), Constants.APP_ID, true);
            //将应用的appid注册到微信
            iwxapi.registerApp(Constants.APP_ID);
        }
        return iwxapi;
    }

    /**
     * 微信登录
     */
    public static void WxLogin() {
        if (!judgeCanGo()) {
            return;
        }
        SendAuth.Req req = new SendAuth.Req();
        //授权域 获取用户个人信息则填写snsapi_userinfo
        req.scope = "snsapi_userinfo";
        //用于保持请求和回调的状态 可以任意填写
        req.state = "test_login";
        iwxapi.sendReq(req);
    }
/* WxShareAndLoginUtils.WxTextShare("测试微信分享",WxShareAndLoginUtils.WECHAT_MOMENT);
            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
            WxShareAndLoginUtils.WxBitmapShare(bitmap,WxShareAndLoginUtils.WECHAT_MOMENT);
            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.logo);
            WxShareAndLoginUtils.WxUrlShare("http://www.baidu.com", "百度", "百度一下", bitmap, WxShareAndLoginUtils.WECHAT_FRIEND);*/

    /**
     * 分享文本至朋友圈
     *
     * @param text  文本内容
     * @param judge 类型选择 好友-WECHAT_FRIEND 朋友圈-WECHAT_MOMENT
     */
    public static void WxTextShare(String text, int judge) {
        if (!judgeCanGo()) {
            return;
        }
        //初始化WXTextObject对象，填写对应分享的文本内容
        WXTextObject textObject = new WXTextObject();
        textObject.text = text;
        //初始化WXMediaMessage消息对象，
        WXMediaMessage message = new WXMediaMessage();
        message.mediaObject = textObject;
        message.mediaTagName = "康科诺商城";
        message.title = "康科诺商城";
        Bitmap bitmap = BitmapFactory.decodeResource(MainApp.getContext().getResources(), R.mipmap.home_nextlevel_topnav_back);
        message.setThumbImage(bitmap);
        //构建一个Req请求对象
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = String.valueOf(System.currentTimeMillis());   //transaction用于标识请求
        req.message = message;
        req.scene = judge;      //分享类型 好友==0 朋友圈==1
        //发送请求
        iwxapi.sendReq(req);
    }

    /**
     * 分享图片
     *
     * @param bitmap 图片bitmap,建议别超过32k
     * @param judge  类型选择 好友-WECHAT_FRIEND 朋友圈-WECHAT_MOMENT
     */
    public static void WxBitmapShare(Bitmap bitmap, int judge) {
        if (!judgeCanGo()) {
            return;
        }
        WXImageObject wxImageObject = new WXImageObject(bitmap);
        WXMediaMessage message = new WXMediaMessage();
        message.mediaObject = wxImageObject;

        Bitmap thunmpBmp = Bitmap.createScaledBitmap(bitmap, 50, 50, true);
        bitmap.recycle();
        message.thumbData = Util.bmpToByteArray(thunmpBmp, true);

        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = String.valueOf(System.currentTimeMillis());
        req.message = message;
        req.scene = judge;

        iwxapi.sendReq(req);

    }

    /**
     * 网页分享
     *
     * @param url         地址
     * @param title       标题
     * @param description 描述
     * @param bitmap      缩略图 不能超过32k
     * @param judge       类型选择 好友-WECHAT_FRIEND 朋友圈-WECHAT_MOMENT
     */
    public static void WxUrlShare(String url, String title, String description, Bitmap bitmap, int judge) {
        if (!judgeCanGo()) {
            return;
        }
        WXWebpageObject wxWebpageObject = new WXWebpageObject();
        wxWebpageObject.webpageUrl = url;

        WXMediaMessage wxMediaMessage = new WXMediaMessage(wxWebpageObject);
        wxMediaMessage.title = title;
        wxMediaMessage.description = description;
        Bitmap thunmpBmp = Bitmap.createScaledBitmap(bitmap, 50, 50, true);
        bitmap.recycle();
        wxMediaMessage.thumbData = Util.bmpToByteArray(thunmpBmp, true);

        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = String.valueOf(System.currentTimeMillis());
        req.message = wxMediaMessage;
        req.scene = judge;

        iwxapi.sendReq(req);
    }

    /*
        * 分享视频
        */
    private void shareVideo(Context shareContent, int shareType) {
        WXVideoObject video = new WXVideoObject();
        video.videoUrl = "";

        WXMediaMessage msg = new WXMediaMessage(video);
        //Bitmap thumb = BitmapFactory.decodeResource(shareContent.getResources(), R.drawable.send_music_thumb);
//      BitmapFactory.decodeStream(new URL(video.videoUrl).openStream());
        /**
         * 测试过程中会出现这种情况，会有个别手机会出现调不起微信客户端的情况。造成这种情况的原因是微信对缩略图的大小、title、description等参数的大小做了限制，所以有可能是大小超过了默认的范围。
         * 一般情况下缩略图超出比较常见。Title、description都是文本，一般不会超过。
         */
        //Bitmap thumbBitmap = Bitmap.createScaledBitmap(thumb, THUMB_SIZE, THUMB_SIZE, true);
        // thumb.recycle();
        // msg.thumbData = Util.bmpToByteArray(thumbBitmap, true);

        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = buildTransaction("video");
        req.message = msg;
        req.scene = shareType;
        iwxapi.sendReq(req);
    }

    private String buildTransaction(final String type) {
        return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
    }

    private static boolean judgeCanGo() {
        getWXAPI();
        if (!iwxapi.isWXAppInstalled()) {
            Toast.makeText(MainApp.getContext(), "请先安装微信应用", Toast.LENGTH_SHORT).show();
            return false;
        } /*else if (!iwxapi.isWXAppSupportAPI()) {
            Toast.makeText(MainApp.getContext(), "请先更新微信应用", Toast.LENGTH_SHORT).show();
            return false;
        }*/
        return true;
    }
}
